/* eslint-disable no-tabs */
/* eslint-disable eqeqeq */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-plusplus */
/* eslint-disable no-var */
/* eslint-disable no-redeclare */
/* eslint-disable consistent-return */
// export function checkHkid(idno) {

//   if (idno.indexOf('(') > 0 && idno.indexOf(')') > 0) {
//     let n = idno.indexOf('(');
//     let idc = ('' + idno).substring(0, n);
//     let ckc = ('' + idno).substring(n + 1, n + 2);
//     let gcc = generateCheckCode(idc);
//     if (gcc && (gcc == ckc)) {
//       return true;
//     }
//   }
//   return false;
// }

export function checkHkid(idno) {
  console.log('idnoidno', idno)
  const reg = new RegExp('[a-zA-Z]');
  const two = (`${idno}`).substring(1, 2);
  if (idno.length > 2 && reg.test(two)) {
    return true;
  }
  if (idno.indexOf('(') > 0 && idno.indexOf(')') > 0) {
    const n = idno.indexOf('(');
    const idc = (`${idno}`).substring(0, n);
    const ckc = (`${idno}`).substring(n + 1, n + 2);
    const gcc = generateCheckCode(idc);
    if (gcc && (gcc == ckc)) {
      return true;
    }
  }
  return false;
}

function generateCheckCode(idno) {
  const alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  let i; let a; let d; let sum = 0; let s = '';
  var idno;
  if (idno.length != 7) return;

  if (valid(idno) == 1) {
    a = idno.charAt(0).toUpperCase();
    sum = 58 * 9 + (alpha.indexOf(a) + 10) * 8;
    s = `58*9 +${alpha.indexOf(a) + 10}*8`
    for (i = 1; i <= 6; i++) {
      sum += idno.charAt(i) * (8 - i);
      s += ` + ${idno.charAt(i)}*${8 - i}`;
    }
    d = sum % 11;
    if (d == 0) d = '0';
    else if (d == 1) d = 'A';
    else d = 11 - d;
    return d;
  }
}

function valid(idno) {	// A123456
  const a = idno.charAt(0).toUpperCase();
  if (idno.length != 7 || a < 'A' || a > 'Z') return 0;
  for (let i = 1; i < idno.length; i++) { if (idno.charAt(i) < '0' || idno.charAt(i) > '9') return 0; }
  return 1;
}
